﻿using System;
using System.Collections.Generic;
using DCS.Domain.Entities;
using DCS.Infrastructure;
using NHibernate.Criterion;

namespace Dcs.Infrastructure
{
    public class Repository : SessionProvider
    {
        public Repository(string connectionString)
            : base(connectionString)
        {
        }

        public void Save(object data)
        {
            using (var session = GetSession())
            {                
                session.Save(data);
                session.Flush();
            }
        }

        public void Update(object data)
        {
            using (var session = GetSession())
            {
                session.Update(data);
                session.Flush();
            }
        }

        public void SaveOrUpdate(object data)
        {
            using (var session = GetSession())
            {
                session.SaveOrUpdate(data);
                session.Flush();
            }
        }

        public IList<ComputationalClient> GetClients()
        {
            using (var session = GetSession())
            {
                return session.CreateCriteria(typeof (ComputationalClient))
                    .Add(Restrictions.Between("CoreCount", 100, 200))
                    .List<ComputationalClient>();
            }
        }

        public IList<ComputationalClient> GetClients(Guid clientGuid)
        {
            using (var session = GetSession())
            {
                return session.CreateCriteria(typeof (ComputationalClient))
                    .Add(Restrictions.Eq("Id", clientGuid))
                    .List<ComputationalClient>();
            }
        }
    }
}